/*
  The MIT License
  
  Copyright (c) 2009 smath project
  
  Permission is hereby granted, free of charge, to any person obtaining a copy
  of this software and associated documentation files (the "Software"), to deal
  in the Software without restriction, including without limitation the rights
  to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  copies of the Software, and to permit persons to whom the Software is
  furnished to do so, subject to the following conditions:
  
  The above copyright notice and this permission notice shall be included in
  all copies or substantial portions of the Software.
  
  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  THE SOFTWARE.
*/
package pl.smath.expression.serializer;

import java.util.Iterator;

import pl.smath.expression.model.ExponentiationTerm;
import pl.smath.expression.model.FractionTerm;
import pl.smath.expression.model.NumberTerm;
import pl.smath.expression.model.ProductTerm;
import pl.smath.expression.model.RelationTerm;
import pl.smath.expression.model.RootTerm;
import pl.smath.expression.model.SumTerm;
import pl.smath.expression.model.VariableTerm;
import pl.smath.expression.model.Term;

public class ObjectSerializer {

	/**
	 * Wydruk na konsole
	 * @param term
	 * @return
	 */
	public static String toString(Term term){
		ObjectSerializer os = new ObjectSerializer();
		
		return os.serialize(term);
	}
	
	
	/**
	 * Serializacja
	 * @param term
	 * @return
	 */
	public String serialize(Term term){
     
		if(term instanceof NumberTerm || term instanceof VariableTerm){
			return serializeTerm(term);
		}
		else if(term instanceof SumTerm){
			return serializeSum((SumTerm)term);
		}
		else if(term instanceof ProductTerm){
			return serializeProduct((ProductTerm)term);
		}
		else if(term instanceof FractionTerm){
			return serializeFraction((FractionTerm)term);
		}
		else if(term instanceof ExponentiationTerm){
			return serializeExp((ExponentiationTerm)term);
		}
		else if(term instanceof RelationTerm){
			return serializeRelation((RelationTerm)term);
		}
		else if(term instanceof RootTerm){
			return serializeRoot((RootTerm)term);
		}
		 
		return "";
	}
	 

	/**
	 * Serializacja potegi
	 */
	private String serializeExp(ExponentiationTerm expTerm) {
		String output = "";
		
		if(expTerm.isNegative())
			output = "-";
		
		output += "Exp{" + serialize(expTerm.getBase()) + "}{" +
			serialize(expTerm.getExponent()) + "}";
		
		return output;
	}


	/**
	 * serializacja ulamka
	 */
	private String serializeFraction(FractionTerm fracTerm) {
		String up;
		String down;
		String output;
		
		up = serialize(fracTerm.getNumerator());
		down = serialize(fracTerm.getDenominator());
		output = "Frac{"+ up + "}{" + down + "}";

		if(fracTerm.isNegative())
			output = "-" + output;
		
		return output;
	}


	private String serializeRelation(RelationTerm term) {
		String output;
		
		output = serialize(term.getLeftTerm()) + " " + term.getText() + " " +
			serialize(term.getRightTerm());
		
		return output;
	}


	private String serializeRoot(RootTerm rootTerm) {
		String output = "";
		
		if(rootTerm.isNegative())
			output = "-";
		
		output += "Root{" + serialize(rootTerm.getBase()) + "}{" +
			serialize(rootTerm.getExponent()) + "}";
		
		return output;
	}


	/**
	  * Obsługa Number i Symbol Term
	  * @param term
	  * @return
	  */
	 private String serializeTerm(Term term){
		 return term.toString();
	 }

	 /**
	  * Serializacja sumy
	  * @param term
	  * @return
	  */
	 private String serializeSum(SumTerm sumTerm) {
		String output = "";
		Iterator<Term> terms = sumTerm.getTerms();
		
		
		while(terms.hasNext()){
			Term term = terms.next();
			
			if(output.length() > 0){
				if(!term.isNegative()){
					output += "+";
				}
			}

			output += serialize(term);
		}

		output = "Sum{" + output + "}";
		if(sumTerm.isNegative())
			output = "-" + output;
		
		return output;
	 }


	 /**
	  * Serializacja iloczynu
	  * @param term
	  * @return
	  */
	 private String serializeProduct(ProductTerm productTerm) {
		String output = "";
		Iterator<Term> terms = productTerm.getTerms();
		
		while(terms.hasNext()){
			Term term = terms.next();
			
			if(output.length() > 0){
				output += "*";
			}

			output += serialize(term);
		}
		
		output = "Product{" + output + "}";
		if(productTerm.isNegative())
			output = "-" + output;
		
		return output;
	 }
	 
 }       
